#Region " Imports "

Imports System.Data
Imports BlazeApps.Library.Forum

#End Region

Partial Class Usercontrols_Forum_PendingPosts
    Inherits UserControlBase

#Region " Enums, Memebers and Properties "

    Private DataPageLocal As New PagedDataSource

    Public Property sortCriteria() As String
        Get
            Return CStr(ViewState("sortCriteria"))
        End Get

        Set(ByVal Value As String)
            ViewState("sortCriteria") = Value
        End Set
    End Property

    Public Property sortDir() As String
        Get
            Return CStr(ViewState("sortDir"))
        End Get

        Set(ByVal Value As String)
            ViewState("sortDir") = Value
        End Set
    End Property

    Protected Property ForumGuid() As Guid
        Get
            Return GetGuidFromVS("ForumID")
        End Get
        Set(ByVal value As Guid)
            ViewState.Add("ForumID", value)
        End Set
    End Property

    Protected Property TopicGuid() As Guid
        Get
            Return GetGuidFromVS("TopicID")
        End Get
        Set(ByVal value As Guid)
            ViewState.Add("TopicID", value)
        End Set
    End Property

    Protected Property PostGuid() As Guid
        Get
            Return GetGuidFromVS("PostID")
        End Get
        Set(ByVal value As Guid)
            ViewState.Add("PostID", value)
        End Set
    End Property

    Public Property IsLoadedAsOption() As Boolean
        Get
            Return GetBooleanFromVS("IsLoadedAsOption")
        End Get
        Set(ByVal value As Boolean)
            ViewState.Add("IsLoadedAsOption", value)
        End Set
    End Property

#End Region

#Region " Page Events "

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Me.MessageLabel = Me.uxMessagesLabel
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsLoadedAsOption Then
            SetPageTitle(Resources.Resource.PendingPosts)
        Else
            Me.uxApproveButton.Visible = False
            Me.uxApproveButton.Enabled = False
            Me.uxDenyButton.Visible = False
            Me.uxDenyButton.Enabled = False
        End If

        If IsUserAuthenticated = True Then

            ConfigBaseEditorCtrls()

            If Me.Page.IsPostBack = False Then

				BuildFCKEditor(EditorPageType.ForumEditor)

                'Set Default Sort Order and Direction
                Me.sortCriteria = "topicname"
                Me.sortDir = "asc"

                LoadPage()
            End If

            ConfigBaseNavCtrls()

            If Me.Page.IsPostBack = False Then
                BindDataItems()
            End If

        End If

        If Not IsLoadedAsOption Then
            Me.uxforum_pendingpostsPanel.Visible = ShowControls(SecurityMask.Moderator, True)
        End If

    End Sub

#End Region

#Region " Save Data "

    Private Sub UpdatePost()
        Dim Item As Post = Post.GetPost(PostGuid)
        Dim ForumCheck As New ForumChecks

		Item.PostBody = Me.uxPostTextbox.Value
		Item.UserID = CurrentUserID
		Item.TopicID = TopicGuid
		Item.EditedString = DateTime.Now.ToString
		Item.IsApproved = False
		Item.Save()

	End Sub

	Private Sub Approve()
		Dim PostItem As Post = Post.GetPost(PostGuid)
		Dim TopicItem As Topic = Topic.GetTopic(TopicGuid)

		PostItem.IsApproved = True
		PostItem.Save()

		TopicItem.IsApproved = True
		TopicItem.Save()

		SendPrivateMessage(PostItem.UserID, CurrentUserID, "Post Approved", "Your forum post has been approved and is now available for viewing.")

		BindDataItems()
	End Sub

	Private Sub Deny()
		Dim PostItem As Post = Post.GetPost(PostGuid)

		SendPrivateMessage(PostItem.UserID, CurrentUserID, "Post Denied", "Your forum post has been denied.  Reasons could include inappropriate content, copyrighted material, or other possible reasons.")

		Post.DeletePost(PostGuid)

		BindDataItems()
	End Sub

	Private Sub Delete()
		Dim PostItem As Post = Post.GetPost(PostGuid)

		SendPrivateMessage(PostItem.UserID, CurrentUserID, "Post Deleted", "Your forum post has been deleted.  Reasons could include inappropriate content, copyrighted material, or other possible reasons.")

		Post.DeletePost(PostGuid)

		BindDataItems()
	End Sub

#End Region

#Region " Load Data "

	Private Sub LoadPage()
		LoadPageSize(Me.uxPageSizeDropDownList)
	End Sub

	Private Sub GetForum()
		'Dim Item As Forum = Forum.GetForum(ForumGuid)

		'Me.uxForumNameTextBox.Text = Item.ForumName
		'Me.uxCategoriesDropDownList.SelectedValue = Item.CategoryID.ToString
		'Me.uxDescriptionTextBox.Text = Item.Description
		'Me.uxSortOrderTextbox.Text = Item.SortOrder

		'LoadForumModerators()
		'EnableControls(True)
	End Sub

	Private Sub GetPostDetails()
		Dim Item As Post = Post.GetPost(PostGuid)

		Me.uxPostTextbox.Value = Item.PostBody
	End Sub

	Public Overloads Overrides Sub BindDataItems()

		Dim List As New ForumList
		Dim DT As New DataTable
		Dim DV As New DataView

		If IsLoadedAsOption Then
			DT = List.GetForumPendingPostsByUser(CurrentUserID)
		Else
			DT = List.GetForumPendingPosts(CurrentUserID)
		End If

		Me.RecordCount = DT.Rows.Count

		DV.Table = DT

		DV.Sort = Me.sortCriteria & " " & Me.sortDir

		Me.DataPage.DataSource = DV
		Me.DataPage.AllowPaging = True
		Me.DataPage.PageSize = CType(Me.uxPageSizeDropDownList.SelectedValue.ToString, Integer)
		Me.DataPage.CurrentPageIndex = Me.PageNumber

		CreatePagingLinks()

		With Me.uxResultsGridView
			.DataSource = Me.DataPage
			.DataBind()
		End With

	End Sub

#End Region

#Region " Form Controls Events "

    Protected Sub uxSaveButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxSaveButton.Click
        If Page.IsValid Then
            UpdatePost()
        End If
    End Sub

    Protected Sub EditItem(ByVal sender As Object, ByVal e As CommandEventArgs)
        SetPostGuid(e)

        GetPostDetails()
        Me.uxPostModalPopupExtender.Show()
    End Sub

    Protected Sub ApproveItem(ByVal sender As Object, ByVal e As CommandEventArgs)
        SetPostGuid(e)
        Approve()
    End Sub

    Protected Sub DenyItem(ByVal sender As Object, ByVal e As CommandEventArgs)
        SetPostGuid(e)
        Deny()
    End Sub

    Protected Sub DeleteItem(ByVal sender As Object, ByVal e As CommandEventArgs)
        SetPostGuid(e)
        Delete()
    End Sub

    Protected Sub uxApproveButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxApproveButton.Click

        Dim iI As Integer

        While iI < Me.uxResultsGridView.Rows.Count
            Dim Row As GridViewRow = Me.uxResultsGridView.Rows(iI)
            Dim Checked As Boolean = CType(Row.FindControl("uxSelectCheckBox"), CheckBox).Checked
            ForumGuid = ConvertToGuid(CType(Row.FindControl("uxforumidLabel"), Label).Text.ToString)
            TopicGuid = ConvertToGuid(CType(Row.FindControl("uxtopicidLabel"), Label).Text.ToString)
            PostGuid = ConvertToGuid(CType(Row.FindControl("uxpostidLabel"), Label).Text.ToString)

            If Checked Then
                Approve()
            End If

            System.Math.Min(System.Threading.Interlocked.Increment(iI), iI - 1)

		End While

		BindDataItems()

    End Sub

    Protected Sub uxDenyButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxDenyButton.Click
        Dim iI As Integer

        While iI < Me.uxResultsGridView.Rows.Count
            Dim Row As GridViewRow = Me.uxResultsGridView.Rows(iI)
            Dim Checked As Boolean = CType(Row.FindControl("uxSelectCheckBox"), CheckBox).Checked
            ForumGuid = ConvertToGuid(CType(Row.FindControl("uxforumidLabel"), Label).Text.ToString)
            TopicGuid = ConvertToGuid(CType(Row.FindControl("uxtopicidLabel"), Label).Text.ToString)
            PostGuid = ConvertToGuid(CType(Row.FindControl("uxpostidLabel"), Label).Text.ToString)

            If Checked Then
                Deny()
            End If

            System.Math.Min(System.Threading.Interlocked.Increment(iI), iI - 1)

        End While

		BindDataItems()

    End Sub

    Protected Sub uxResultsGridView_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles uxResultsGridView.Sorting
        'Get the Sort by Value from the Sortexpression field on the column
        If Me.sortCriteria = e.SortExpression Then
            If Me.sortDir = "desc" Then
                Me.sortDir = "asc"
            Else
                Me.sortDir = "desc"
            End If
        End If

        ' Assign the column clicked to the sortCriteria property
        Me.sortCriteria = e.SortExpression

        'Calls the function to rebuild the datagrid view, with the new sort order
        BindDataItems()
    End Sub

    Protected Sub uxResultsGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles uxResultsGridView.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim ALB As LinkButton = CType(e.Row.FindControl("uxApproveItemLinkButton"), LinkButton)
            Dim DLB As LinkButton = CType(e.Row.FindControl("uxDenyItemLinkButton"), LinkButton)

            If IsLoadedAsOption Then
                ALB.Enabled = False
                ALB.Visible = False
                DLB.Enabled = False
                DLB.Visible = False
            End If

        End If
    End Sub

#End Region

#Region " Helpers and Methods "

    Private Sub ConfigBaseNavCtrls()

        Me.PagingTopPanel = Me.uxPagingTopPanel
        Me.PagingBottomPanel = Me.uxPagingBottomPanel

        Me.RepeaterTop = Me.uxPagesTopRepeater
        Me.RepeaterBottom = Me.uxPagesBottomRepeater

        Me.NextTopLinkButton = Me.uxTopNextLinkButton
        Me.PreviousTopLinkButton = Me.uxTopPrevLinkButton
        Me.NextBottomLinkButton = Me.uxBottomNextLinkButton
        Me.PreviousBottomLinkButton = Me.uxBottomPrevLinkButton

        Me.DataPage = DataPageLocal
        Me.PageSize = CType(Me.uxPageSizeDropDownList.SelectedValue.ToString, Integer)
        Me.PagesToDisplay = 10

        Me.ItemsPerPageDropDown = Me.uxPageSizeDropDownList
        Me.ItemsPerPageLabel = Me.uxItemsPerPageLabel
        Me.PagerInfoLabel = Me.uxPagerInfoLabel

        AddHandler Me.RepeaterTop.ItemCommand, AddressOf PagingRepeater_ItemCommand
        AddHandler Me.RepeaterBottom.ItemCommand, AddressOf PagingRepeater_ItemCommand

    End Sub

    Private Sub ConfigBaseEditorCtrls()
		Me.FCKEditor = Me.uxPostTextbox
    End Sub

    Private Sub SetPostGuid(ByVal e As CommandEventArgs)
        Dim Item As String() = Split(e.CommandArgument.ToString, "|")

        TopicGuid = ConvertToGuid(Item(0).ToString().Trim())
        PostGuid = ConvertToGuid(Item(1).ToString().Trim())
    End Sub

#End Region

End Class
